﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitesDAL
{
    public class GiaovienDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Giaovien");
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Giaovien {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Giaovien)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Giaovien '{0}', N'{1}', '{2}', '{3}', N'{4}', '{5}', N'{6}' ";
            sql = string.Format(sql, o.MaGV, o.Hoten, o.Gioitinh, o.Namsinh, o.Diachi, o.Dienthoai, o.Trinhdo);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Giaovien)obj;
            var sql = "set dateformat dmy; execute sp_Update_Giaovien '{0}', N'{1}', '{2}', '{3}', N'{4}', '{5}', N'{6}' ";
            sql = string.Format(sql, o.MaGV, o.Hoten, o.Gioitinh, o.Namsinh, o.Diachi, o.Dienthoai, o.Trinhdo);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"select * from tb_Giaovien where Hoten like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }


        public DataTable Search(DAL.Entities.Giaovien o)
        {
            string sql;
            if (o.MaGV != "")
                sql = @"Select *, (Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex from tb_Giaovien Where MaGV like '%" + o.MaGV + "%' and Hoten like N'%" + o.Hoten + "%' and Diachi like N'%" + o.Diachi + "%' and Dienthoai like '%" + o.Dienthoai + "%' and Trinhdo like '%"+o.Trinhdo+"%'  ";
            else
                sql = @"Select *, (Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex from tb_Giaovien Where Hoten like N'%" + o.Hoten + "%' and Diachi like N'%" + o.Diachi + "%' and Dienthoai like '%" + o.Dienthoai + "%' and Trinhdo like '%" + o.Trinhdo + "%'  ";
            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(MaGV) as Ma From tb_Giaovien";
            return ExecuteQuery(sql);
        }

        #endregion
    }
}
